package com.hawk.eye.client.toolkit;

import ch.qos.logback.classic.LoggerContext;
import com.hawk.eye.client.constant.HawkEyeConstant;
import com.hawk.eye.common.constant.AuthConstant;
import com.hawk.eye.common.model.ServiceAuthData;
import com.hawk.eye.common.utils.JsonUtil;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author zd
 */
public class ServiceAuthHolder {

    public static void setAuthData(ServiceAuthData sad) {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.putProperty(ServiceAuthData.class.getSimpleName(), JsonUtil.toJson(sad));
    }

    public static ServiceAuthData getAuthData() {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        return JsonUtil.parse(context.getProperty(ServiceAuthData.class.getSimpleName()), ServiceAuthData.class);
    }

    public static void setServerAddr(String serverAddr) {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.putProperty(HawkEyeConstant.SERVER_ADDR, serverAddr);
    }

    public static String getServerAddr() {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        return context.getProperty(HawkEyeConstant.SERVER_ADDR);
    }

    public static boolean isHealthy() {
        return getAuthData() != null;
    }

}